package com.virus.mall.controller.mall;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.virus.mall.model.Result;
import com.virus.mall.model.bo.AskGoodsMsgBo;
import com.virus.mall.model.vo.*;
import com.virus.mall.service.GoodsService;
import com.virus.mall.service.impl.GoodsServiceImpl;
import com.virus.mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Classname MallGoodsServlet
 * @Description NULL
 * @Date 2021/4/20 14:29
 * @Created by virus
 */
@WebServlet("/api/mall/goods/*")
public class MallGoodsServlet extends HttpServlet {

    private GoodsService goodsService = new GoodsServiceImpl();

    private ObjectMapper objectMapper = new ObjectMapper();

    {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replaceAll("/api/mall/goods/", "");
        if (op.equals("askGoodsMsg")) {
            askGoodsMsg(request, response);
        }
    }

    /**
     * 在商品问答提问
     * @param request
     * @param response
     * @throws IOException
     */
    private void askGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AskGoodsMsgBo askGoodsMsgBo = null;
        try {
            askGoodsMsgBo = objectMapper.readValue(requestBody, AskGoodsMsgBo.class);
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("参数转化错误")));
            return;
        }
        boolean result = goodsService.askGoodsMsg(askGoodsMsgBo);
        if (result) {
            response.getWriter().println(objectMapper.writeValueAsString(Result.ok()));
        } else {
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("提问失败")));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replaceAll("/api/mall/goods/", "");
        if (op.startsWith("getGoodsByType")) {
            getGoodsByType(request, response);
        } else if (op.startsWith("getGoodsInfo")) {
            getGoodsInfo(request, response);
        } else if (op.startsWith("getGoodsMsg")) {
            getGoodsMsg(request, response);
        } else if (op.startsWith("searchGoods")) {
            searchGoods(request, response);
        } else if (op.startsWith("getGoodsComment")) {
            getGoodsComment(request, response);
        }
    }

    /**
     * 根据类型获得商品
     * @param request
     * @param response
     * @throws IOException
     */
    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeIdStr = request.getParameter("typeId");
        int typeId;
        try {
            typeId = Integer.parseInt(typeIdStr);
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("参数类型错误")));
            return;
        }
        List<GoodsGetByTypeVo> goodsByType = goodsService.getGoodsByType(typeId);
        response.getWriter().println(objectMapper.writeValueAsString(Result.ok(goodsByType)));
    }

    /**
     * 获得商品信息
     * @param request
     * @param response
     * @throws IOException
     */
    private void getGoodsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idStr = request.getParameter("id");
        int id;
        try {
            id = Integer.parseInt(idStr);
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("参数类型错误")));
            return;
        }
        GoodsGetInfoMallVo goodsGetInfoMallVo = goodsService.getMallGoodsInfo(id);
        if (goodsGetInfoMallVo == null) {
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("该商品不存在")));
        }
        response.getWriter().println(objectMapper.writeValueAsString(Result.ok(goodsGetInfoMallVo)));
    }

    /**
     * 获得商品问答信息
     * @param request
     * @param response
     * @throws IOException
     */
    private void getGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idStr = request.getParameter("id");
        int id;
        try {
            id = Integer.parseInt(idStr);
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("参数类型错误")));
            return;
        }
        List<GetGoodsMsgVo> getGoodsMsgVos = goodsService.getGoodsMsg(id);
        response.getWriter().println(objectMapper.writeValueAsString(Result.ok(getGoodsMsgVos)));
    }

    /**
     * 模糊查询商品信息
     * @param request
     * @param response
     * @throws IOException
     */
    private void searchGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String keyword = request.getParameter("keyword");
        List<SearchGoodsVo> searchGoodsVos = goodsService.searchGoods(keyword);
        response.getWriter().println(objectMapper.writeValueAsString(Result.ok(searchGoodsVos)));
    }

    /**
     * 获得商品评论信息
     * @param request
     * @param response
     * @throws IOException
     */
    private void getGoodsComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String goodsIdStr = request.getParameter("goodsId");
        int goodsId;
        try {
            goodsId = Integer.parseInt(goodsIdStr);
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("传入参数错误")));
            return;
        }
        GetGoodsCommentVo getGoodsCommentVo = goodsService.getGoodsComment(goodsId);
        response.getWriter().println(objectMapper.writeValueAsString(Result.ok(getGoodsCommentVo)));
    }
}
